package com. hafu. view. GYS;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import com. hafu. model. GysInfo;
import com. hafu. service. GysInfoService;
import com. hafu. util. StringUtil;
import java. awt.*;
import java. awt. event.*;
import java. util. ArrayList;
import java. util. Vector;
import javax. swing.*;
import javax. swing. GroupLayout;
import javax. swing. table.*;
public class ManageGYSFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();

    public ManageGYSFrm() {
        initComponents();
        fillTabel();
    }

    private void fillTabel() {
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(new GysInfo());
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        for (GysInfo gysInfo : gysInfoList) {
            Vector<Object> rowData = new Vector<>();
            rowData.add(gysInfo.getId());
            rowData.add(gysInfo.getName());
            rowData.add(gysInfo.getTel());
            model.addRow(rowData);
        }
    }

    private void button1(ActionEvent e) {
        String name = textField1.getText();
        GysInfo gysInfo = new GysInfo();
        gysInfo.setName(name);
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(gysInfo);
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        if (gysInfoList.size() > 0) {
            for (GysInfo gysInfo1 : gysInfoList) {
                Vector<Object> rowData = new Vector<>();
                rowData.add(gysInfo1.getId());
                rowData.add(gysInfo1.getName());
                rowData.add(gysInfo1.getTel());
                model.addRow(rowData);
            }
        } else {
            JOptionPane.showMessageDialog(null, "没有查询到供应商信息");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseld = (int) dtm.getValueAt(row, 0);
        String name = (String) dtm.getValueAt(row, 1);
        String tel = (String) dtm.getValueAt(row, 2);
        textField2.setText(name);
        textField3.setText(tel);
    }

    private void button2(ActionEvent e) {
        String name = textField2.getText();
        if (StringUtil.isEmpty(name)) {
            JOptionPane.showMessageDialog(null, "供应商名称不能为空");
            return;
        }
        String tel = textField3.getText();
        if (StringUtil.isEmpty(tel)) {

            JOptionPane.showMessageDialog(null, "供应商电话不能为空");
            return;
        }
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseld = (int) dtm.getValueAt(row, 0);
        GysInfo gysInfo = new GysInfo(chooseld, name, tel);
        Boolean aBoolean = gysInfoService.updateGysInfo(gysInfo);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null, "修改成功");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }

    private void button3(ActionEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseld = (int) dtm.getValueAt(row, 0);
        Boolean isDeleteGysInfo = gysInfoService.deleteGysInfo(chooseld);
        if (isDeleteGysInfo) {
            JOptionPane.showMessageDialog(null, "删除成功");
            fillTabel();
        } else {
            JOptionPane.showMessageDialog(null, "删除失败");
        }
    }

    private void initComponents() {
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();
        setTitle("\u7ba1\u7406\u4f9b\u5e94\u5546");
        Container contentPane = getContentPane();
        label1.setText("\u4f9b\u5e94\u5546\u540d\u5b57");
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));
        {
            table1.setModel(new DefaultTableModel(
                    new Object[][]{
                            {null, null, null},

                            {null, null, null},
                    },
                    new String[]{
                            "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                    }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }
        label2.setText("\u4f9b\u5e94\u5546\u540d\u5b57");
        label3.setText("\u4f9b\u5e94\u5546\u7535\u8bdd");
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));
        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                .addContainerGap(106, Short.MAX_VALUE)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 556, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(201, Short.MAX_VALUE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                                                .addGap(41, 41, 41)
                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
                                                .addGap(185, 185, 185)
                                                .addComponent(button1))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(label3)
                                                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))
                                                .addGap(35, 35, 35)
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                                                        .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                                                .addGap(156, 156, 156)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(button3)
                                                        .addComponent(button2))))
                                .addContainerGap(265, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(button1)
                                        .addComponent(label1)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                                GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(button2)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                                GroupLayout.PREFERRED_SIZE))
                                .addGap(33, 33, 33)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label3)
                                        .addComponent(button3)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                                GroupLayout.PREFERRED_SIZE))
                                .addGap(62, 62, 62))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }

    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JButton button2;


    private JPanel panel1;

    private JButton button3;


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - wmy
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button2 = new JButton();
        button3 = new JButton();
        button1 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u4f9b\u5e94\u5546\u540d\u5b57");
        contentPane.add(label1);
        label1.setBounds(120, 30, 70, 30);
        contentPane.add(textField1);
        textField1.setBounds(225, 30, 125, textField1.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u4f9b\u5e94\u5546\u540d\u5b57");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(100, 340), label2.getPreferredSize()));
        contentPane.add(textField2);
        textField2.setBounds(205, 330, 145, textField2.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u4f9b\u5e94\u5546\u7535\u8bdd");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(100, 390), label3.getPreferredSize()));
        contentPane.add(textField3);
        textField3.setBounds(205, 380, 145, textField3.getPreferredSize().height);

        //======== panel1 ========
        {
            panel1.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax
            .swing.border.EmptyBorder(0,0,0,0), "JFor\u006dDesi\u0067ner \u0045valu\u0061tion",javax.swing
            .border.TitledBorder.CENTER,javax.swing.border.TitledBorder.BOTTOM,new java.awt.
            Font("Dia\u006cog",java.awt.Font.BOLD,12),java.awt.Color.red
            ),panel1. getBorder()));panel1. addPropertyChangeListener(new java.beans.PropertyChangeListener(){@Override
            public void propertyChange(java.beans.PropertyChangeEvent e){if("bord\u0065r".equals(e.getPropertyName(
            )))throw new RuntimeException();}});
            panel1.setLayout(null);

            //======== scrollPane1 ========
            {

                //---- table1 ----
                table1.setModel(new DefaultTableModel(
                    new Object[][] {
                        {null, null, null},
                    },
                    new String[] {
                        "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                    }
                ));
                table1.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        table1MouseClicked(e);
                    }
                });
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(0, 0, scrollPane1.getPreferredSize().width, 255);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(new Rectangle(new Point(55, 70), panel1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(395, 330), button2.getPreferredSize()));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));
        contentPane.add(button3);
        button3.setBounds(new Rectangle(new Point(395, 380), button3.getPreferredSize()));

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(390, 30), button1.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(535, 460));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - wmy
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button2;
    private JButton button3;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
